﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KTool2.Utility;
using KTool2.Common;

namespace KTool2.Impl
{
    public class KanjiWord : ICloneable
    {
        private int id;
        private int memRate;
        private string category;
        private string kanji;
        private string hiragana;
        private string english;
        private string hanNom;
        private string viet;

        public KanjiWord(int id, int memRate, string category)
        {
            this.id = id;
            this.memRate = memRate;
            this.category = category;
        }

        public KanjiWord(int id, int memRate, string category, string kanji, string hiragana, string english, string hanNom, string viet)
        {
            this.id = id;
            this.memRate = memRate;
            this.category = category;
            this.kanji = kanji;
            this.hiragana = hiragana;
            this.english = english;
            this.hanNom = hanNom;
            this.viet = viet;
        }

        private KanjiWord(KanjiWord other)
        {
            this.id = other.id;
            this.memRate = other.memRate;

            if (other.category != null)
            {
                this.category = string.Copy(other.category);
            }

            if (other.kanji != null)
            {
                this.kanji = string.Copy(other.kanji);
            }

            if (other.hiragana != null)
            {
                this.hiragana = string.Copy(other.hiragana);
            }

            if (other.english != null)
            {
                this.english = string.Copy(other.english);
            }

            if (other.hanNom != null)
            {
                this.hanNom = string.Copy(other.hanNom);
            }

            if (other.viet != null)
            {
                this.viet = string.Copy(other.viet);
            }
        }

        /// <summary>
        /// ID of kanji word
        /// </summary>
        public int ID
        {
            get { return this.id; }
        }

        /// <summary>
        /// String of this kanji
        /// </summary>
        public string Kanji
        {
            get 
            {
                if (this.kanji == null)
                { 
                    this.kanji = KanjiWordProcessor.GetKanjiInfo(this.category, this.id, KanjiProperties.KANJI);
                }

                return this.kanji;
            }
        }

        /// <summary>
        /// Hiragana of this kanji
        /// </summary>
        public string Hiragana
        {
            get 
            {
                if (this.hiragana == null)
                { 
                    this.hiragana = KanjiWordProcessor.GetKanjiInfo(this.category, this.id, KanjiProperties.HIRAGANA);
                }

                return this.hiragana;
            }
            set { this.hiragana = value; }
        }

        /// <summary>
        /// English meaning of this kanji
        /// </summary>
        public string English
        {
            get 
            {
                if (this.english == null)
                { 
                    this.english = KanjiWordProcessor.GetKanjiInfo(this.category, this.id, KanjiProperties.ENGLISH); 
                }

                return this.english;
            }
            set { this.english = value; }
        }

        /// <summary>
        /// Hán Nôm of this Kanji
        /// </summary>
        public string HanNom
        {
            get 
            {
                if (this.hanNom == null)
                { 
                    this.hanNom = KanjiWordProcessor.GetKanjiInfo(this.category, this.id, KanjiProperties.HANNOM); 
                }

                return this.hanNom;
            }
            set { this.hanNom = value; }
        }

        /// <summary>
        /// Việt meaning of this kanji
        /// </summary>
        public string Viet
        {
            get 
            {
                if (this.viet == null)
                { 
                    this.viet = KanjiWordProcessor.GetKanjiInfo(this.category, this.id, KanjiProperties.VIET); 
                }

                return this.viet;
            }
            set { this.viet = value; }
        }

        /// <summary>
        /// Rating for level of memory
        /// </summary>
        public int MemRate
        {
            get { return this.memRate; }
            set
            {
                if (this.memRate != value)
                {
                    KanjiWordProcessor.SetMemRate(this.Category, this.ID, value);
                    this.memRate = value;
                }
            }
        }

        /// <summary>
        /// In database, it's table name or sheet name
        /// </summary>
        public string Category
        {
            get { return this.category; }
        }

        /// <summary>
        /// Release kanji, hiragana, english, han nom, viet on memory
        /// </summary>
        public void Release()
        {
            this.kanji = null;
            this.hiragana = null;
            this.english = null;
            this.hanNom = null;
            this.viet = null;

            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        ///// <summary>
        ///// Swap internal properties with other kanji word
        ///// </summary>
        ///// <param name="other">other kanji word to be swaped with</param>
        //public void SwapWith(KanjiWord other)
        //{
        //    if (this.id != null)
        //    {
        //        int temp = this.id;
        //        this.id = other.ID;
        //        other.id = temp;
        //    }

        //    if (this.category != null)
        //    {
        //        string temp = this.category;

        //    }
        //}

        public object Clone()
        {
            return new KanjiWord(this);
        }
    }
}
